import { HttpClient } from "@angular/common/http";
import { Component, OnInit } from "@angular/core";
import { ActivatedRoute } from "@angular/router";
import { MenuController, NavController } from "@ionic/angular";
@Component({
  selector: "app-cate",
  templateUrl: "./cate.page.html",
  styleUrls: ["./cate.page.scss"],
})
export class CatePage implements OnInit {
  constructor(
    public http: HttpClient,
    public router: NavController,
    public route: ActivatedRoute,
    public menu: MenuController
  ) {}
  res: Result;
  nextP: number;
  bool = "false";
  nid = this.route.snapshot.params.num_id;
  title = this.route.snapshot.params.title;
  cate: Cate;
  doRefresh(e) {
    this.bool = "false";
    let pno = this.nid - 1;
    if (this.nid > 1) {
      let url =
        "http://xiaoshuogebed.applinzi.com/cate_sec?title=" +
        this.title +
        "&num_id=" +
        pno;

      this.http.get(url).subscribe((res: Result) => {
        this.res = res;
        this.nid = pno;
        e.target.complete();
      });
    } else {
      let url =
        "http://xiaoshuogebed.applinzi.com/cate_sec?title=" +
        this.title +
        "&num_id=" +
        1;

      this.http.get(url).subscribe((res: Result) => {
        this.res = res;
        e.target.complete();
      });
    }
  }

  change(num_id) {
    this.bool = "false";
    let url =
      "http://xiaoshuogebed.applinzi.com/cate_sec?title=" +
      this.title +
      "&num_id=" +
      num_id;
    this.http.get(url).subscribe((res: Result) => {
      this.res = res;
      this.nid = Number(num_id);
      this.nextP = Number(num_id);
    });

    this.menu.close();
  }
  openMenu() {
    this.menu.open();
  }

  loadData(e) {
    this.nextP = Number(this.nid) + 1;
    let url =
      "http://xiaoshuogebed.applinzi.com/cate_sec?title=" +
      this.title +
      "&num_id=" +
      this.nextP;
    this.http.get(url).subscribe((res: Result) => {
      if (this.nextP == res.results.length + Number(this.nid)) {
        res.results = this.res.results.concat(res.results);
        this.res = res;
        this.nid = this.nextP;
      } else {
        this.bool = "true";
      }
      e.target.complete();
    });
  }
  ngOnInit() {
    let url =
      "http://xiaoshuogebed.applinzi.com/cate_sec?title=" +
      this.title +
      "&num_id=" +
      this.nid;
    this.http.get(url).subscribe((res: Result) => {
      this.res = res;
    });
    let cateUrl = "http://xiaoshuogebed.applinzi.com/cate?title=" + this.title;
    this.http.get(cateUrl).subscribe((result: Cate) => {
      this.cate = result;
    });
  }
}
interface Result {
  code: number;
  message: string;
  results: Any[];
}
interface Any {
  content: string;
  num_id: string;
  section: string;
  title: string;
}
interface Cate {
  code: number;
  message: string;
  results: Cates[];
}
interface Cates {
  bid: number;
  cid: number;
  content: string;
  num_id: string;
  title: string;
  section: string;
}
